package ${package.ServiceImpl};

import com.github.pagehelper.PageInfo;
import ${package.Entity}.${entity};
import ${cfg.reqPackage}.${cfg.addReqName};
import ${cfg.reqPackage}.${cfg.deleteReqName};
import ${cfg.reqPackage}.${cfg.editReqName};
import ${cfg.reqPackage}.${cfg.getListReqName};
import ${cfg.reqPackage}.${cfg.getInfoReqName};
import ${cfg.resPackage}.BaseOperationRes;
import com.hyzx.qbasic.common.util.OperationResUtils;
import com.hyzx.qbasic.common.util.UuidUtils;
import ${cfg.repositoryPackage}.${cfg.repositoryName};
import ${package.Service}.${table.serviceName};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * ${table.comment!} 服务实现类
 *
 * @author ${author}
 * @since ${date}
 */
<#assign entityVarName ="${cfg.entityName?uncap_first}">
<#assign repositoryVarName ="${entityVarName}Repository">
<#assign searchFieldCapitalName ="${cfg.searchField?cap_first}">
<#--获取主键字段-->
<#list table.fields as field>
    <#if field.keyFlag>
        <#assign primaryKey = field>
    </#if>
</#list>

@Service
public class ${table.serviceImplName} implements ${table.serviceName} {
    @Autowired
    private ${cfg.repositoryName} ${repositoryVarName};

    @Override
    public PageInfo<${entity}> getPaged${cfg.entityName}List(${cfg.getListReqName} req) {
        PageInfo<${entity}> ${entityVarName}List = ${repositoryVarName}.getPaged${cfg.entityName}List(
            req.getPageNum(), req.getPageSize(), req.get${searchFieldCapitalName}());

        return ${entityVarName}List;
    }

    @Override
    public ${entity} get${cfg.entityName}InfoById(${cfg.getInfoReqName} req) {
        return ${repositoryVarName}.get${cfg.entityName}ById(req.get${primaryKey.capitalName}());
    }

    @Override
    public BaseOperationRes add${cfg.entityName}(${cfg.addReqName} req) {
        ${entity} ${entityVarName} = new ${entity}();
        <#list table.fields as field>
            <#if !field.keyFlag>
                <#if field.propertyName!="createTime" && field.propertyName!="updateTime">
                    <#if field.propertyType == "boolean">
                        <#assign getprefix="is"/>
                    <#else>
                        <#assign getprefix="get"/>
                    </#if>
        ${entityVarName}.set${field.capitalName}(req.${getprefix}${field.capitalName}());
                </#if>
            <#else>
                <#if field.propertyType == "String">
        ${entityVarName}.set${field.capitalName}(UuidUtils.getObjectId());
                </#if>
            </#if>
        </#list>
        ${entityVarName}.setCreateTime(new Date());
        ${entityVarName}.setUpdateTime(new Date());

        boolean success = ${repositoryVarName}.add${cfg.entityName}(${entityVarName});
        if (!success)
            return OperationResUtils.fail("新增失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes edit${cfg.entityName}(${cfg.editReqName} req) {
        //检查记录是否存在
        ${entity} existed${cfg.entityName} = ${repositoryVarName}.get${cfg.entityName}ById(req.get${primaryKey.capitalName}());
        if (existed${cfg.entityName} == null)
            return OperationResUtils.fail("记录不存在");

        ${entity} ${entityVarName} = new ${entity}();
        <#list table.fields as field>
                <#if field.propertyName!="createTime" && field.propertyName!="updateTime">
                    <#if field.propertyType == "boolean">
                        <#assign getprefix="is"/>
                    <#else>
                        <#assign getprefix="get"/>
                    </#if>
        ${entityVarName}.set${field.capitalName}(req.${getprefix}${field.capitalName}());
                </#if>
        </#list>
        ${entityVarName}.setUpdateTime(new Date());

        boolean success = ${repositoryVarName}.edit${cfg.entityName}(${entityVarName});
        if (!success)
            return OperationResUtils.fail("编辑失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes delete${cfg.entityName}(${cfg.deleteReqName} req) {
        boolean success = ${repositoryVarName}.delete${cfg.entityName}ByIds(req.get${primaryKey.capitalName}List());
        if (!success)
            return OperationResUtils.fail("删除失败");

        return OperationResUtils.success();
    }
}